/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SonarLightCounter;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.sensor.*;
import javax.microedition.media.*;
import javax.microedition.io.*;
import sUtils.*;



/**
 * @author Administrator
 */
public class SonarLightCounter extends MIDlet implements DataListener  {
    LightCanvas canvas;
    SonarThread thread;
    SensorConnection sensor;
    Player player=null;
    int m_show=10;
    boolean m_sonarStart=false;
    double ox, oy;

    public SonarLightCounter() {
        Lang.init();
        canvas = new LightCanvas(this);
        thread = new SonarThread(this);
        SensorInfo[] sensors = SensorManager.findSensors(null,null);
        for ( int i=0; i<sensors.length; i++ )
        {
            if (sensors[i].getQuantity().equalsIgnoreCase("acceleration"))
            {
                try {
                    sensor = (SensorConnection) Connector.open(sensors[i].getUrl());
                    sensor.setDataListener(this, 1);
                } catch (Exception e) { }
                break;
            }
        }
    }
    public void startApp()
    {
        Display.getDisplay(this).setCurrent( canvas );
        thread.start();
    }
    public void pauseApp() {}
    public void destroyApp(boolean unconditional) {}
    public void close()
    {
        try { sensor.close(); } catch(Exception e) {}
        Display.getDisplay(this).setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }
    public void sonar()
    {
        if (m_show==0) Display.getDisplay(this).flashBacklight(10);
        m_show = 10;
        m_sonarStart=true;
    }
    public void sonarUpdate()
    {
        if (m_sonarStart)
        {
            canvas.change(true);
            try {
                /*
                InputStream is = getClass().getResourceAsStream("/sonar.mp3");
                player = Manager.createPlayer(is, "audio/mp3");
                player.setLoopCount(1);
                player.realize();
                player.start();
                */
                Manager.playTone(97, 200, 100);
                Display.getDisplay(this).vibrate(60);
                Manager.playTone(97, 300, 20);

            } catch(Exception e) { player = null; Display.getDisplay(this).vibrate(300); }
            m_sonarStart=false;
        }
    }
    public void update()
    {
        if (m_show>0) {
            m_show--; if (m_show==0) canvas.change(false);
        }
    }
    public void dataReceived( SensorConnection conn, Data[] arg, boolean lost )
    {
        double x = arg[0].getDoubleValues()[0];
        double y = arg[1].getDoubleValues()[0];
        double z = arg[2].getDoubleValues()[0];

        double dx = ox>x ? ox-x : x-ox;
        double dy = oy>y ? oy-y : y-oy;

        if ((dx+dy)>10 && m_show==0) { sonar(); }
        ox=x; oy=y;
    }

    class SonarThread extends Thread {
        public boolean m_exit = false;
        Player player = null;
        SonarLightCounter parent = null;

        public SonarThread( SonarLightCounter p ) { parent = p; }
        public void run()
        {
            while (!m_exit)
            {
                for ( int i=0; i<10; i++ )
                {
                    parent.sonarUpdate();
                    try { sleep(100); } catch(Exception e) { return; }
                }
                parent.update();
            }
        }
    }


    class LightCanvas extends Canvas {
        int m_counter = 0;
        int bgcolor = 0xffffff;
        int textcolor = 0;
        SonarLightCounter parent=  null;
        LightCanvas( SonarLightCounter p )
        {
            parent = p;
            setFullScreenMode(true);
        }
        public void paint( Graphics g ) {
            int w = getWidth();
            int h = getHeight();
            String s = "" + m_counter;
            Font f = Font.getFont( Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
            g.setColor(bgcolor);
            g.fillRect(0,0,w,h);
            g.setFont( f );
            g.setColor(textcolor);
            g.drawString(s, w/2 - f.stringWidth(s)/2, h/2-f.getBaselinePosition()/2, 0);
            f = Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
            g.setFont(f);

            g.setColor(0);
            g.fillRect(w-50,h-30,50,30);
            g.fillRect(0,h-30,50,30);
            g.setColor(0x808080);
            g.drawString(Lang.word("Quit","Ende"), w-48, h-28, 0);
            g.drawString(Lang.word("Back","Zurück"), 2, h-28, 0);
        }
        public void change( boolean on ) { bgcolor = on ? 0xffffff : 0; textcolor = on ? 0 : 0xffffff; repaint(); }
        public void add() { m_counter++; parent.sonar(); }
        public void sub() { if (m_counter>0) m_counter--; repaint(); }
        public void pointerPressed( int x, int y )
        {
            int w = getWidth();
            int h = getHeight();
            if (x>(w-50) && y>(h-30)) parent.close(); else
            if (x<50 && y>(h-30)) sub(); else add();
        }
    }
}
